/**   
 * @Title: CommonWebView.java 
 * @author 7828wc   
 * @date 2016年1月21日 下午4:15:17 
 */
package com.onstar.cn.weight;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.onstar.cn.common.log.LogManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 7828wc
 *
 */
public class CommonWebView extends WebView {
	private static  final  String TAG = "CommonWebView";
	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public CommonWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initData(this,context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public CommonWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initData(this,context);
	}

	/**
	 * @param context
	 */
	public CommonWebView(Context context) {
		super(context);
		initData(this,context);
	}

	@SuppressLint("NewApi")
	protected void setupDefaultSettings(WebView webview,Context context) {
		// WebSettingManager wsm = WebSettingManager.getInstance();
		WebSettings settings = webview.getSettings();
		// settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		// settings.setCacheMode(WebSettings.LOAD_NORMAL);

		settings.setLightTouchEnabled(false);
		settings.setNeedInitialFocus(false);

		// settings.setLoadsImagesAutomatically(wsm.getIsLoadImage());
		settings.setJavaScriptEnabled(true);

		settings.setSupportZoom(false);
		settings.setBuiltInZoomControls(false);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			settings.setDisplayZoomControls(false);
		} else {

		}
		settings.setDefaultTextEncodingName("GBK");
		settings.setLoadWithOverviewMode(true);
		settings.setUseWideViewPort(true);

		settings.setRenderPriority(WebSettings.RenderPriority.NORMAL);

		settings.setGeolocationEnabled(true);
		settings.setDatabaseEnabled(true);
		settings.setDomStorageEnabled(true);
		settings.setAppCacheEnabled(true);

		// long appCacheMaxSize =
		// UmeBrowserActivity.myself.getWebStorageSizeManagerInstance().getAppCacheMaxSize();
		String databasePath = context.getDir("databases", 0).getPath();
		String geolocationDatabasePath = context.getDir("geolocation", 0).getPath();
		String appCachePath = context.getDir("appcache", 0).getPath();

		settings.setGeolocationDatabasePath(geolocationDatabasePath);
		settings.setDatabasePath(databasePath);
		// settings.setAppCacheMaxSize(appCacheMaxSize);
		settings.setAppCachePath(appCachePath);
		settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
		settings.setSavePassword(true);
		settings.setSaveFormData(true);

		// settings.setPageCacheCapacity(10);
		reflectPageCacheTo(settings, 20);
		// settings.setUserAgentString(wsm.getUAString());

		settings.setSupportMultipleWindows(true);

		settings.setJavaScriptCanOpenWindowsAutomatically(true);

		// reflectPrivateBrowsingEnable(settings,
		// wsm.getPrivateBrowsingEnable());

		// settings.setTextSize(mapIntToTextSize(wsm.getTextSize()));
		// mapTextSizeToZoom(webview, mapIntToTextSize(wsm.getTextSize()));

		// Set the nav dump for HTC 2.x devices (disabling for ICS, deprecated
		// entirely for Jellybean 4.2)
		try {
			Method gingerbread_getMethod = WebSettings.class.getMethod("setNavDump", boolean.class);

			String manufacturer = android.os.Build.MANUFACTURER;
			Log.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);
			if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB
					&& android.os.Build.MANUFACTURER.contains("HTC")) {
				gingerbread_getMethod.invoke(settings, true);
			}
		} catch (NoSuchMethodException e) {
			Log.d(TAG, "We are on a modern version of Android, we will deprecate HTC 2.3 devices in 2.8");
		} catch (IllegalArgumentException e) {
			Log.d(TAG, "Doing the NavDump failed with bad arguments");
		} catch (IllegalAccessException e) {
			Log.d(TAG, "This should never happen: IllegalAccessException means this isn't Android anymore");
		} catch (InvocationTargetException e) {
			Log.d(TAG, "This should never happen: InvocationTargetException means this isn't Android anymore.");
		}
	}

	private void reflectPageCacheTo(WebSettings setting, int capacity) {
		try {
			Method setpgcache = setting.getClass().getDeclaredMethod("setPageCacheCapacity", int.class);
			setpgcache.setAccessible(true);
			setpgcache.invoke(setting, capacity);
		} catch (NoSuchMethodException e) {
			LogManager.getInstance().e(e.getMessage());
		} catch (IllegalArgumentException e) {
			LogManager.getInstance().e(e.getMessage());
		} catch (IllegalAccessException e) {
			LogManager.getInstance().e(e.getMessage());
		} catch (InvocationTargetException e) {
			LogManager.getInstance().e(e.getMessage());
		}
	}

	private void initData(WebView webView,Context context) {
		setupDefaultSettings(webView,context);
	}
}
